<!--
 * @Description: 班组定义
 * @Autor: 何兴家
 * @Date: 2020-10-27 15:24:13
 * @LastEditors: 何兴家
 * @LastEditTime: 2020-11-12 09:29:25
-->

<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="4" labelWidth="90" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="hxrWorkGroup"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="columns => (this.selectColumn = columns)"
      selectionType="single"
    >
      <template slot="moreActions">
        <span @click="removeRecordHandle">删除数据</span>
      </template>
      <template slot="controls" >
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新建</el-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '30px'}"
    >
      <editWorkerGroup :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getBrands } from '@/api/common/cars/brandcarsModels';
import { getWorkerGroups, getOrgList,deleteWorkgroup } from '@/api/repair/repairBusiness/instituteDefined';
import editWorkerGroup from './tabs/editWorkerGroup';
import { confirmAction, notifyAction } from '@/utils';

export default {
  name: 'WorkGroup',
  components: {
    editWorkerGroup
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新建', edit: '编辑', show: '显示' };
    return {
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: getWorkerGroups,
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      orgList: [],
      selectColumn: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    // this.getOrgList();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          label: '班组代码',
          fieldName: 'workgroupCode',
          placeholder: '请输入...',
          type:'INPUT'
        },
        {
          label: '班组名称',
          fieldName: 'workgroupName',
          placeholder: '请输入...',
          type:'INPUT'
        },
        // {
        //   label: '部门',
        //   fieldName: 'orgNo',
        //   placeholder: '请选择...',
        //   type:'SELECT',
        //   itemList:[]
        // },
        {
          label: '工位类别',
          fieldName: 'workerTypeCode',
          placeholder: '请选择...',
          type:'SELECT',
          itemList:this.createDictList('8041')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 130,
          fixed: 'left',
          render: (props,h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>编辑</el-button>
              </div>
            );
          }
        },
        {
          title: '班组代码',
          dataIndex: 'workgroupCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '班组名称',
          dataIndex: 'workgroupName',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '部门',
        //   dataIndex: 'orgNo',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems:[],
        //   editable: false,
        //   editType: 'select',
        //   editItems: [],
        //   showOverflowTooltip: true
        // },
        {
          title: '工种类型',
          dataIndex: 'workerTypeCode',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems:this.createDictList('8041'),
          render: (props,h) => {
            return <span> {this.createDictText(props.row.workerTypeCode, '8041')} </span>;
          }
        },
        {
          title: '班组状态',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '启用', value: 10041001 },
            { text: '停用', value: 10041002 }
          ],
          disabled: true,
          dictItems: [
            { text: '启用', value: 10041001 },
            { text: '停用', value: 10041002 }
          ]
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}班组信息`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    // setOrgList(props) {
    //   let ret = props.row.orgNo;
    //   this.orgList.forEach(x => {
    //     if (x.orgNo == props.row.orgNo){
    //       ret = x.text;
    //     }
    //   });
    //   return ret;
    // },
    async getOrgList(){
      let res = (await getOrgList({orgType:15061004})).data;
      console.log(res,'kkkk')
     this.columns.find(x => x.dataIndex === 'orgNo').filterItems= this.columns.find(x => x.dataIndex === 'orgNo').editItems = res.map(x => ({ text: x.orgName, value: +x.orgId }));
     this.topFilterList.find(x => x.fieldName === 'orgNo').itemList = res.map(x => ({ text: x.orgName, value: +x.orgId }));
      // console.log('部門',this.columns.find(x => x.dataIndex === 'orgNo').editItems);
    },
    async removeRecordHandle() {
      try {
        await confirmAction('此操作将删除该记录, 是否继续?');
        console.log(this.selectColumn);
        let workgroupId = this.selectColumn[0].workgroupId;
        let res = await deleteWorkgroup(this.selectColumn[0],workgroupId);
        if (res.resultCode === 200) {
          notifyAction('删除数据成功！');
          // 执行刷新
          this.refreshHandle();
        }
        // else {
        //   this.$notify.error({ title: '提示信息', message: res.errMsg || '删除班主报错!' });
        // }
      } catch (e) {}
    },
    refreshHandle() {
      this.fetchParams = { ...this.fetchParams };
    }
  }
};
</script>

<style lang="less" scoped>
